/*
 * ====================================================================================
 * Copyright 2012 Eric Hartwell  (http://www.ehartwell.com)
 * ====================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
 * this file except in compliance with the License. You may obtain a copy of the
 * License at http://www.apache.org/licenses/LICENSE-2.0. Unless required by applicable
 * law or agreed to in writing, software distributed under the License is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 * ====================================================================================
 */

package com.ehartwell.samples.android.pagerview;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.ImageView;

/**
 * Page fragment containing a Gallery control.
 */
public class GalleryFragment extends PagerFragment {

    private final boolean   mIsSmart;
    protected Gallery       mGallery;
    protected ImageView     mZoom;

    /**
     * Creates the page fragment.
     *
     * @param isSmart <tt>True</tt> to pass swipe events though to the gallery.
     */
    public GalleryFragment(boolean isSmart) {
        super();
        mIsSmart = isSmart;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.gallery_fragment, null);
        
        mZoom = (ImageView)view.findViewById(R.id.zoom);
        
        mGallery = (Gallery)view.findViewById(R.id.gallery);
        mGallery.setAdapter(new ImageAdapter(view.getContext()));

        mGallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {
                mZoom.setImageResource((Integer)mGallery.getAdapter().getItem(position));
           }
        });
        
        if (!mIsSmart)  view.setBackgroundColor(Color.LTGRAY);
        return view;
    }

    @Override
    /**
     * Gets the title string.
     *
     * @return Title string, or <tt>null</tt> if none available.
     */
    public String getTitle() {
        return mIsSmart ? "Smart Gallery" : "Dumb Gallery";
    }

    /**
     * Determines if the motion event should be handled inside this fragment, such as by a Gallery control.
     *
     * @param motionEvent The motion event being dispatched down the hierarchy.
     * @return <tt>True</tt> if the motion event should be handled inside this fragment.
     */
    public boolean isLocalTouchEvent(MotionEvent motionEvent) {
        return mIsSmart && isEventInsideView(motionEvent, mGallery);
    }

}
